package net.zjitc.homework.hw1;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Administrator
 * @Date 2020/5/19 13:36
 **/
@WebServlet("/storyServlet")
public class StoryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*1、获取请求头referer（告诉服务器是从哪里过来的）
        * 2、判断，若是从本项目地址过来的，
        *       则请求转发到storyDetail.html
        *    否则，转发到storyError.html
        * */
        String referer = req.getHeader("referer");
        /* 获取项目资源路径*/
        String contextPath = req.getContextPath();
        if(referer!=null){
            if(referer.contains(contextPath)){
                /* 转发到storyDeatil.html*/
                req.getRequestDispatcher("/hw/storyDetail.html").forward(req,resp);
            }else{
                req.getRequestDispatcher("/hw/storyError.html").forward(req,resp);
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
